package com.runfast.paotui.web.controller.server;


import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.paotui.dao.model.PricePlan;
import com.runfast.paotui.dao.model.PricePlanExample;
import com.runfast.paotui.service.PricePlanService;
import com.runfast.waimai.dao.model.BaseUser;
import com.runfast.waimai.dao.model.WaiMaiRunfastAgentaccounts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController("pricePlanController")
@RequestMapping("/server/pricePlan")
public class PricePlanController {
    @Autowired
    private PricePlanService  pricePlanService;
    /**
     * 保存方案
     */
    @PostMapping("/save")
    public Result save(@RequestBody PricePlan pricePlan){
        BaseUser baseUser= SessionContext.getCurrentBaseUser();//后台用户
        WaiMaiRunfastAgentaccounts runfastAgentaccounts= SessionContext.getCurrentRunfastAgentaccounts();//代理商账号
        if(baseUser !=null){
            pricePlan.setCreateBy(baseUser.getId());
        }else if(runfastAgentaccounts !=null){
            pricePlan.setCreateBy(runfastAgentaccounts.getId());
            pricePlan.setAgentId(runfastAgentaccounts.getAgentid());
        }
        pricePlan.setCreateTime(new Date());
        pricePlanService.insert(pricePlan);
        return Result.ok(null);
    }

    /**
     * 修改方案
     */
    @PostMapping("/update")
    public Result update(@RequestBody PricePlan pricePlan){
        BaseUser baseUser= SessionContext.getCurrentBaseUser();//后台用户
        WaiMaiRunfastAgentaccounts runfastAgentaccounts= SessionContext.getCurrentRunfastAgentaccounts();//代理商账号
        if(baseUser !=null){
            pricePlan.setUpdateBy(baseUser.getId());
        }else if(runfastAgentaccounts !=null){
            pricePlan.setUpdateBy(runfastAgentaccounts.getId());
        }
        pricePlan.setUpdateTime(new Date());
        pricePlanService.updateByPrimaryKeySelective(pricePlan);
        return Result.ok(null);
    }

    /**
     * 删除方案
     */
   @PostMapping("/delete")
    public Result delete(Integer id){
       if(id ==null){
           return Result.fail(null,"Id不能为空！");
       }



       PricePlan pricePlan=new PricePlan();
       pricePlan.setDeleted(true);
       PricePlanExample example=new PricePlanExample();
       example.createCriteria().andIdEqualTo(id);
       pricePlanService.updateByExampleSelective(pricePlan,example);
       return Result.ok(null);
    }

    /**
     * 所有方案列表
     */
    @GetMapping("/list")
    public Result list(){
        PricePlanExample example=new PricePlanExample();
        BaseUser baseUser= SessionContext.getCurrentBaseUser();//后台用户
        WaiMaiRunfastAgentaccounts runfastAgentaccounts= SessionContext.getCurrentRunfastAgentaccounts();//代理商账号
        if(baseUser !=null){
            //example.createCriteria().andAgentIdEqualTo(null);
        }else if(runfastAgentaccounts !=null){
            example.createCriteria().andAgentIdEqualTo(runfastAgentaccounts.getAgentid());
        }
        example.createCriteria().andDeletedNotEqualTo(true);
        List<PricePlan> pricePlanList = pricePlanService.selectByExample(example);
        PricePlan defaultPlan=queryByDefaultPlan(runfastAgentaccounts.getAgentid());

        Map<Object,Object> map=new HashMap<Object, Object>();
        map.put("defaultPlan",defaultPlan);
        map.put("list",pricePlanList);
        return Result.ok(null,map);
    }

    /**
     * 方案信息
     */
    @GetMapping("/info")
    public Result info(Integer id){
        PricePlan pricePlan = pricePlanService.selectByPrimaryKey(id);
        return Result.ok(null,pricePlan);
    }

    /**
     * 设置为默认方案
     */
    @PostMapping("/default")
    public Result update(Integer id){
        WaiMaiRunfastAgentaccounts runfastAgentaccounts= SessionContext.getCurrentRunfastAgentaccounts();//代理商账号
        PricePlan defaultPlan=queryByDefaultPlan(runfastAgentaccounts.getAgentid());
        if(defaultPlan !=null){
            PricePlan pricePlan1=new PricePlan();
            pricePlan1.setDefaultPlan(false);
            PricePlanExample example1=new PricePlanExample();
            example1.createCriteria().andIdEqualTo(defaultPlan.getId());
            pricePlanService.updateByExampleSelective(pricePlan1,example1);
        }
        PricePlan pricePlan=new PricePlan();
        pricePlan.setDefaultPlan(true);
        PricePlanExample example=new PricePlanExample();
        example.createCriteria().andIdEqualTo(id);
        pricePlanService.updateByExampleSelective(pricePlan,example);
        return Result.ok(null);
    }
    /**
     * 查询当前代理商的默认跑腿价格方案
    * */
    public PricePlan queryByDefaultPlan(Integer agentid){
        PricePlan pricePlan=new PricePlan();
        PricePlanExample example=new PricePlanExample();
        example.createCriteria().andDefaultPlanEqualTo(true);
        List<PricePlan> list=pricePlanService.selectByExample(example);
        if(list.size()>0){pricePlan=list.get(0);} else{pricePlan=null;}
        return pricePlan;
    }


}
